package com.ipc.admin.service.zzcool.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ipc.admin.service.zzcool.service.AnnouncementService;
import com.ipc.common.pojo.zzcool.bo.AnnouncementBo;
import com.ipc.common.pojo.vo.R;
import com.ipc.core.dao.zzcool.AnnouncementDao;
import com.ipc.core.entity.zzcool.Announcement;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * (TAnnouncement)表服务实现类
 *
 * @author makejava
 * @since 2023-04-28 11:36:07
 */
@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Resource
    private AnnouncementDao dao;

    @Override
    public R save(Announcement announcement) {
        if (announcement != null) {
            announcement.setTime(new DateTime());
            if (dao.insert(announcement) > 0) {
                return R.ok("添加成功");
            }
        }
        return R.fail("添加失败");
    }

    @Override
    public R updateById(Announcement announcement) {
        if (announcement != null) {
            announcement.setTime(new DateTime());
            if (dao.updateById(announcement) > 0) {
                return R.ok("修改成功");
            }
        }
        return R.fail("修改失败");
    }

    @Override
    public R del(int id) {
        if (id > 0) {
            if (dao.deleteById(id) > 0) {
                return R.ok("删除成功");
            }
        }
        return R.fail("删除失败");
    }

    @Override
    public R queryById(int id) {
        if (id > 0) {
            Announcement announcement = dao.selectById(id);
            if (announcement != null) {
                return R.ok(announcement);
            }
        }
        return R.fail("查询失败");
    }

    @Override
    public R queryAllWhere(AnnouncementBo bo) {
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
//        wrapper.like("title", bo.getTitle());
//        wrapper.like("content", bo.getContent());
//        wrapper.eq("state", bo.getState());
        List<Announcement> list = dao.selectList(wrapper.like("title", bo.getTitle())
                .like("content", bo.getContent()).eq("state", bo.getState()));
        if (list.size() > 0) {
            return R.ok(list);
        }
        return R.fail();
    }

    @Override
    public R queryAll() {
        List<Announcement> list = dao.selectList(null);
        return R.ok(list);
    }

}


